package org.jeecg.modules.app.v1.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.aspct.ApiCheckDataAuth;
import org.jeecg.modules.app.v1.device.entity.ApiDeviceCategoryEntity;
import org.jeecg.modules.app.v1.device.entity.ApiDeviceEntity;
import org.jeecg.modules.app.v1.device.entity.ApiFaultDeviceEntity;
import org.jeecg.modules.app.v1.device.entity.ApiShopDeviceEntity;
import org.jeecg.modules.aspect.ApiLoginAspct;
import org.jeecg.modules.power.device.service.IYlDeviceRecordsService;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description: 设备表
* @Author: jeecg-boot
* @Date:   2024-07-16
* @Version: V1.0
*/
@Api(tags="APP管理端_设备数据统计")
@RestController
@RequestMapping("/api/v1/device/data")
@Slf4j
@RequiredArgsConstructor
public class ApiDeviceDataController {

   private final IYlDeviceService ylDeviceService;
   private final IYlDeviceRecordsService deviceRecordsService;

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="1_资产一览", notes="资产一览")
   @PostMapping(value = "/getAll")
   @ApiOperationSupport(order = 1)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getAll(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getAllDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="2_资产一览_下级数据", notes="资产一览_下级数据")
   @PostMapping(value = "/getBelowAll")
   @ApiOperationSupport(order = 2)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getBelowAll(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getBelowDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }
   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="3_资产一览_故障设备_下级数据", notes="资产一览_故障设备_下级数据")
   @PostMapping(value = "/getFaultDevice")
   @ApiOperationSupport(order = 3)
   @ApiCheckDataAuth
   public Result<List<ApiDeviceCategoryEntity>> getFaultDevice(String flag,String groupId,HttpServletRequest request){
      List<ApiDeviceCategoryEntity> list = ylDeviceService.getFaultDeviceByCategory(flag, groupId);
      return Result.OK(list);
   }

   /**
    * 资产一览接口待确认
    */
   @ApiOperation(value="4_资产一览_故障设备_BD/门店下钻接口", notes="资产一览_故障设备_下级数据")
   @PostMapping(value = "/faultDeviceList")
   @ApiImplicitParams({
           @ApiImplicitParam(name="categoryId",value = "设备分类ID"),
           @ApiImplicitParam(name="flag",value = "1小组2BD"),
           @ApiImplicitParam(name="groupId",value = "flag=1时小组Id，flag=2时bdId"),
           @ApiImplicitParam(name="mac",value = "设备mac，模糊搜索"),
           @ApiImplicitParam(name="sourceType",value = "2BD3门店"),
           @ApiImplicitParam(name="status",value = "1待审核2已完成"),
   })
   @ApiOperationSupport(order = 4)
   @ApiCheckDataAuth
   public Result<List<ApiFaultDeviceEntity>> faultDeviceList(String categoryId,String flag,String groupId, String mac,String sourceType,String status,
                                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request){
      Page<ApiFaultDeviceEntity> page = new Page<>(pageNo,pageSize);
      IPage<ApiFaultDeviceEntity> list = deviceRecordsService.faultDeviceList(page,mac,categoryId,flag, groupId,sourceType,status);
      return Result.OK(list.getRecords());
   }


   /**
    *  资产BD
    * @param request
    * @return
    */
   @ApiOperation(value="4_资产BD", notes="资产BD")
   @PostMapping(value = "/bdProperty")
   @ApiOperationSupport(order = 4)
   @ApiCheckDataAuth
   public Result<?> bdProperty( String categoryId,String flag,String groupId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.bdDeviceList(page,categoryId,flag,groupId);
      return Result.OK(list);
   }
   /**
    * 资产门店
    * @param request
    * @return
    */
   @ApiOperation(value="5_资产门店", notes="资产门店")
   @PostMapping(value = "/shopProperty")
   @ApiOperationSupport(order = 5)
   @ApiCheckDataAuth
   public Result<?> shopProperty(String categoryId,String flag,String groupId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.shopDeviceList(page,categoryId,flag,groupId);
      return Result.OK(list);
   }

   /**
    * 资产仓库
    * @param request
    * @return
    */
   @ApiOperation(value="6_资产仓库", notes="资产仓库")
   @PostMapping(value = "/storeProperty")
   @ApiOperationSupport(order = 6)
   @ApiCheckDataAuth
   public Result<?> storeProperty(String categoryId,String flag,String groupId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
      Page<ApiDeviceEntity> page = new Page<>(pageNo,pageSize);
      List<ApiDeviceEntity> list = ylDeviceService.storeDeviceList(page,categoryId);
      return Result.OK(list);
   }

   @ApiLogin
   @ApiOperation(value="7_扫码校验新增门店绑定设备", notes="扫码校验新增门店绑定设备")
   @GetMapping(value = "/check")
   @ApiOperationSupport(order = 7)
   @ApiImplicitParams({
           @ApiImplicitParam(value = "设备编号",name = "deviceMac"),
   })
   public Result<?> check(String deviceMac,HttpServletRequest request) {
      SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
      JSONObject result = deviceRecordsService.getNewDevice(deviceMac,bd.getId());
      if(oConvertUtils.isEmpty(result))return Result.error_599("未查询到该设备信息");
      return Result.OK(result);
   }

   @ApiLogin
   @ApiOperation(value="8_门店设备列表（需添加具体设备在线情况）", notes="门店设备列表")
   @GetMapping(value = "/shopDeviceList")
   @ApiOperationSupport(order = 8)
   public Result<?> shopDeviceList(String shopId) {
      List<ApiShopDeviceEntity> result = ylDeviceService.queryShopDevice(shopId);
      return Result.OK(result);
   }

}
